const Router = require("koa-router");
//拿到操作user表的逻辑对象
const user = require("../control/user");

//拿到操作文章列表的逻辑对象
const article = require("../control/article");

//拿到操作评论的列表的逻辑对象
const comment = require("../control/comment");

const router = new Router;

//设计主页
router.get("/", user.keepLog, article.getList);

//正则匹配路由,因为登录和注册都在一个页面
router.get(/^\/user\/(?=reg|login)/,async (ctx, err)=>{
    //show 为true则显示注册，false显示登录
    const show = /reg$/.test(ctx.path);
    await ctx.render("register",{show});
});

//处理用户登录的 post请求
router.get("/user/login",async (ctx, err)=>{
  console.log(ctx.request.body);
  const data= ctx.request.body;
});

//用户注册
router.post("/user/reg",user.reg);

//用户登录
router.post("/user/login",user.login);

//用户退出
router.get("/user/logout",user.logout);

//文章的发表页面
router.get("/article",user.keepLog,article.addPage);

//文章添加
router.post("/article",user.keepLog,article.add);

//文章列表分页查询
router.get("/page/:id", article.getList);

//文章详情页
router.get("/article/:id", user.keepLog, article.details);

//发表评论
router.post("/comment", user.keepLog, comment.addComment)

module.exports = router;
